﻿using System;
using System.Windows.Forms;
using MobileCEPistolet.WebReference;

namespace MobileCEPistolet
{
    public partial class Form1 : Form
    {
        string user = "";
        string haslo = "";
        string magazynier = "";
        string document = "";
        private string role = "kasjer";
        public string Role
        {
            get { return this.role; }
            set { this.role = value; }
        }

        private int boxNumber = 1;
        public int BoxNumber
        {
            get { return this.boxNumber; }
            set { this.boxNumber = value; }
        }
        public Service1 Client
        {
            set;
            get;
        }
        private Service1 GetClient()
        {
            if (Client == null)
            {
                Client = new Service1();
            }
            return Client;
        }
        public Form1()
        {
            InitializeComponent();
            this.tabControl1.TabPages.RemoveAt(1);
            this.tbUser.Focus();
        }

        private void cbMagazynier_Click(object sender, EventArgs e)
        {

        }

        private void cbLista_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {

        }

        private void btnOpen_Click_1(object sender, EventArgs e)
        {
            if (cbDokumenty.Text != "" && cbMagazynier.Text != "")
            {
                btnAccept.Visible = true;
                btnClose.Visible = true;
                btnOpen.Visible = false;
                //fillDGV(cbLista.Text);
                //lblInput.Visible = true;
                //tbInput.Visible = true;
                cbDokumenty.Visible = false;
                lblDoc.Visible = false;
                //tbInput.Focus();
                magazynier = cbMagazynier.Text;
                document = cbDokumenty.Text;
                cbMagazynier.Visible = false;
                lblMagazynier.Visible = false;
                //btnjKartonSkanowanie.Visible = true;
                //btnDrukujEtykiety.Visible = true;
                ////Sprawdzam ostatni dodany karton dla dokumentu
                //string querySelectLastBox = "SELECT max( a.BOX_NUMBER) FROM BOX_DOCUMENTS a where a.DOCUMENT_ID like " + this.cbLista.Text;
                //OdbcCommand comSelectLastBox = new OdbcCommand(querySelectLastBox, con);
                //OdbcDataReader readerSelectLastBox = comSelectLastBox.ExecuteReader();
                //if (readerSelectLastBox.Read())
                //{
                //    this.BoxNumber = Convert.ToInt32(readerSelectLastBox[0]);
                //}
                //this.lbBoxNumberSkan.Text = "Skanujemy do pudełka " + this.BoxNumber;
                //pist.blockKasjerDocument(document, "use", con);//blokowanie dokumentu do edycji
                //pist.logToBase("open", document, magazynier, user, DateTime.UtcNow.ToString(), con);
            }
            else
            {
                MessageBox.Show("Wybierz dokument", "Informacja", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            btnOpen.Visible = true;
            btnAccept.Visible = false;
            btnClose.Visible = false;
            //dgv.Visible = false;
            //dgv.DataSource = new DataSet();
            cbDokumenty.Text = "";
            //lblInput.Visible = false;
            //tbInput.Visible = false;
            cbDokumenty.Visible = true;
            lblDoc.Visible = true;
            //pist.blockKasjerDocument(document, "clear", con);
            //pist.logToBase("close", document, magazynier, user, DateTime.UtcNow.ToString(), con);
            magazynier = "";
            document = "";
            cbMagazynier.Visible = true;
            lblMagazynier.Visible = true;
        }

        private void btnLoguj_Click(object sender, EventArgs e)
        {
            this.tbxErrorInfoLogowanie.Text = string.Empty;
            Service1 client = GetClient();
            client.Timeout = 300000;
            this.GetClient().Url = "http://192.168.2.33:82/Service1.svc";
            user = tbUser.Text;
            haslo = tbPasswd.Text;
            bool logSuccess = false;
            bool test = false;
            try
            {
                this.GetClient().Loguj(user, haslo, new string[] { Helpers.Konfigurator("serwer", "konfiguratorPistolety.xml"),
                    Helpers.Konfigurator("baza", "konfiguratorPistolety.xml"), 
                    Helpers.Konfigurator("charset", "konfiguratorPistolety.xml"), 
                    Helpers.Konfigurator("role", "konfiguratorPistolety.xml") }, out logSuccess, out test);
                //this.tbUser.Text = client.GetData(this.tbUser.Text).Lista[0];
            }
            catch (Exception ex)
            {
                this.tbxErrorInfoLogowanie.Text = ex.Message + @"/\ result: " + logSuccess.ToString();
            }
            finally
            {
                if (test)
                {
                    tabControl1.TabPages.RemoveAt(0);
                    tabControl1.TabPages.Add(tabPageSkanowanie);
                }
            }
        }

        private void menuItemZamknij_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemWyloguj_Click(object sender, EventArgs e)
        {
            this.GetClient().Dispose();
            this.tabControl1.TabPages.RemoveAt(0);
            this.tabControl1.TabPages.Add(tabPageLogowanie);
            this.tabControl1.Update();
        }

        private void cbDokumenty_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void cbDokumenty_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void cbDokumenty_GotFocus(object sender, EventArgs e)
        {
            try
            {
                MobileCEPistolet.WebReference.TablicaDanych data = new MobileCEPistolet.WebReference.TablicaDanych();
                this.cbDokumenty.Items.Clear();
                string cs = this.GetClient().GetData();
                data = this.GetClient().GetListDocuments();
                this.cbDokumenty.DataSource = data.Tablica;
                this.cbDokumenty.DisplayMember = "Value";
                this.cbDokumenty.ValueMember = "Key";
                this.cbDokumenty.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbPasswd_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnLoguj_Click(sender, e);
            }
        }
    }
}